package com.hb.znpt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.hb.znpt.BR;
import com.hb.znpt.R;
import com.hb.znpt.adapter.LandAdapter;
import com.hb.znpt.bean.LandBean;
import com.hb.znpt.bean.LesseeBean;
import com.hb.znpt.databinding.ActivityLandBinding;
import com.hb.znpt.viewmodel.LandViewModel;
import com.unitlib.base.base.BaseActivity;

import java.util.List;

public class LandActivity extends BaseActivity<ActivityLandBinding, LandViewModel> {

    private LandAdapter adapter;
    private LesseeBean bean;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_land;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        initBackTitle(binding.toolbar);
        initRecyclerView();
        bean = (LesseeBean) getIntent().getSerializableExtra("data");
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getData();
    }

    @Override
    public void initViewObservable() {
        viewModel.listData.observe(this, new Observer<List<LandBean>>() {
            @Override
            public void onChanged(List<LandBean> beans) {
                adapter.setList(beans);
            }
        });
    }

    /**
     * Init recycler view.
     * 初始化RecyclerView
     */
    public void initRecyclerView() {
        adapter = new LandAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                LandBean landBean = (LandBean) adapter.getData().get(position);
                Intent intent = new Intent(LandActivity.this, UploadActivity.class);
                intent.putExtra("dept", bean);
                intent.putExtra("data", landBean);
                startActivity(intent);
            }
        });
    }
}
